package com.contest;

public class Solutioin2909 {
    public int minimumSum(int[] nums) {
        int[] minBack = new int[nums.length];
        minBack[nums.length - 1] = nums[nums.length - 1];
        for (int i = nums.length - 2; i > 0; i--) {
            minBack[i] = Math.min(nums[i + 1], minBack[i + 1]);
        }
        int res = Integer.MAX_VALUE;
        int pre = nums[0];
        for (int i = 1; i < nums.length - 1; i++) {
            if (nums[i] > pre && nums[i] > minBack[i]) {
                res = Math.min(res, pre + nums[i] + minBack[i]);
            }
            pre = Math.min(pre, nums[i]);
        }
        return res == Integer.MAX_VALUE ? -1 : res;
    }
}
